import GridSystem from "../GridSystem"
import IPoint from "../interface/IPoint";
import Block from "./Block"
import { ControlPoint } from "../Constants";

class MyControlPoint extends Block {
    name: string;
    constructor(x: number, y: number) {  // MyControlPoint的宽高不受比例限制
        super(x, y, 10, 10);
        this.isFixedSize = true;
        this.name = "";
    }

    update(x?: number, y?: number) {
        if (x) this.x = GridSystem.gls.getRelativePosX(x, this);
        if (y) this.y = GridSystem.gls.getRelativePosY(y, this);
    }

    drawPonit(points: IPoint[], parentCenter?: IPoint) {  // 绘制
        let ctx = GridSystem.gls.ctx;
        ctx.save()
        ctx.beginPath();

        if (this.angle != 0 && this.parentBlock && parentCenter) {   // 跟着父元素旋转
            ctx.translate(parentCenter.x, parentCenter.y)
            ctx.rotate(this.angle * Math.PI / 180)
            ctx.translate(-parentCenter.x, -parentCenter.y)
        }

        ctx.fillStyle = "red";
        ctx.moveTo(points[0].x, points[0].y);
        ctx.lineTo(points[1].x, points[1].y);
        ctx.lineTo(points[2].x, points[2].y);
        ctx.lineTo(points[3].x, points[3].y);

        ctx.closePath(); //闭合
        ctx.fill();	//再填充
        ctx.restore()
        this.update(points[0].x, points[0].y)
    }

    getStartPos(x: number, y: number, rectWidth: number, rectHeight: number): IPoint {   // 参数全是实际像素
        switch (this.name) {
            case ControlPoint.LEFT_TOP_POINT:
                return this.getLeftTopPointPosition(x, y)[0];
            case ControlPoint.RIGHT_TOP_POINT:
                return this.getRightTopPointPosition(x, y, rectWidth, rectHeight)[0]
            case ControlPoint.LEFT_BOTTOM_POINT:
                return this.getLeftBottomPointPosition(x, y, rectWidth, rectHeight)[0]
            case ControlPoint.RIGHT_BOTTOM_POINT:
                return this.getRightBottomPointPosition(x, y, rectWidth, rectHeight)[0]
            case ControlPoint.ROTATE_POINT:
                return this.getRotatePointPosition(x, y, rectWidth, rectHeight)[0]
            default:
                return {
                    x: 0,
                    y: 0
                }
        }
    }

    // 根据四个点的坐标不同，去获得不同的绘制点， 第一个点作为起始点
    getLeftTopPointPosition(x: number, y: number): IPoint[] {
        return [
            { x: x - this.width / 2, y: y - this.height / 2 },
            { x: x + this.width / 2, y: y - this.height / 2 },
            { x: x + this.width / 2, y: y + this.height / 2 },
            { x: x - this.width / 2, y: y + this.height / 2 },
        ]
    }

    getRightTopPointPosition(x: number, y: number, width: number, height: number): IPoint[] {
        return [
            { x: x - this.width / 2 + width, y: y - this.height / 2 },
            { x: x + this.width / 2 + width, y: y - this.height / 2 },
            { x: x + this.width / 2 + width, y: y + this.height / 2 },
            { x: x - this.width / 2 + width, y: y + this.height / 2 },
        ]
    }
    getLeftBottomPointPosition(x: number, y: number, width: number, height: number): IPoint[] {
        return [
            { x: x - this.width / 2, y: y - this.height / 2 + height },
            { x: x + this.width / 2, y: y - this.height / 2 + height },
            { x: x + this.width / 2, y: y + this.height / 2 + height },
            { x: x - this.width / 2, y: y + this.height / 2 + height },
        ]

    }
    getRightBottomPointPosition(x: number, y: number, width: number, height: number): IPoint[] {
        return [
            { x: x - this.width / 2 + width, y: y - this.height / 2 + height },
            { x: x + this.width / 2 + width, y: y - this.height / 2 + height },
            { x: x + this.width / 2 + width, y: y + this.height / 2 + height },
            { x: x - this.width / 2 + width, y: y + this.height / 2 + height },
        ]
    }

    getRotatePointPosition(x: number, y: number, width: number, height: number): IPoint[] {
        return [
            { x: x - this.width / 2 + width / 2, y: y - this.height / 2 - ControlPoint.ROTATE_DIST },
            { x: x + this.width / 2 + width / 2, y: y - this.height / 2 - ControlPoint.ROTATE_DIST },
            { x: x + this.width / 2 + width / 2, y: y + this.height / 2 - ControlPoint.ROTATE_DIST },
            { x: x - this.width / 2 + width / 2, y: y + this.height / 2 - ControlPoint.ROTATE_DIST },
        ]
    }
}

export default MyControlPoint;